/**
 * Created by jihb on 4/20/16.
 */
Ext.define('JYnovel.controller.system.MenuController',{
    extend:'Ext.app.Controller',
    views:[
        'system.MenuView',
        'system.MenuForm'
    ],
    models:[
        'system.MenuModel'
    ],
    stores:[
        'system.MenuStore'
    ],
    refs:[{
        ref:'MenuGroupGridView',
        selector:'MenuGroupGridView'
    },{
        ref:'MenuGridView',
        selector:'MenuGridView'
    },{
        ref:'MenuFuncGridView',
        selector:'MenuFuncGridView'
    },{
        ref:'MenuRightMenu',
        selector:'MenuRightMenu'
    }],
    init:function () {
        this.control({
            'MenuGroupGridView':{
                afterrender:this.menuGroupGridViewAfterrender,
                select:this.menuGroupGridViewSelect,
                itemcontextmenu:this.menuGroupGridViewItemcontextmenu
            },
            'MenuGridView':{
                afterrender:this.menuGridViewAfterrender,
                select:this.menuGridViewSelect,
                itemcontextmenu:this.menuGridViewItemcontextmenu
            },
            'MenuFuncGridView':{
                afterrender:this.menuFuncGridViewAfterrender,
                itemcontextmenu:this.menuFuncGridViewItemcontextmenu
            },
            'MenuGroupGridView button[name=menuGropAddBtn]':{
                click:this.menuGropAddAction
            },
            'MenuGridView button[name=menuAddBtn]':{
                click:this.menuAddAction
            },
            'MenuFuncGridView button[name=menuFuncAddBtn]':{
                click:this.menuFuncAddAction
            },
            'MenuForm button[name=addMenuOkBtn]':{
                click:this.addMenuOkBtnClick
            },
            'MenuRightMenu menuitem[name=menuEditMenuItem]':{
                click:this.menuEditMenuItemClick
            },
            'MenuRightMenu menuitem[name=menuDeleteMenuItem]':{
                click:this.menuDeleteMenuItemClick
            }
        });


    },
    menuGroupGridViewAfterrender:function (grid,eOpts) {
        var store = grid.getStore();
        store.on('beforeload',function () {
            store.getProxy().setExtraParam('parentId',0);
        });
        store.load();
    },
    menuGridViewAfterrender:function (grid,eOpts) {
        var me = this;
        var store = grid.getStore();
        store.on('beforeload',function () {
            var groupMenuRecord = ExtGetSelectRow(me.getMenuGroupGridView());
            store.getProxy().setExtraParam('parentId',groupMenuRecord.get('id'));
            store.getProxy().setExtraParam('level',2);
        });
    },
    menuFuncGridViewAfterrender:function (grid,eOpts) {
        var me = this;
        var store = grid.getStore();
        store.on('beforeload',function () {
            var menuRecord = ExtGetSelectRow(me.getMenuGridView());
            store.getProxy().setExtraParam('parentId',menuRecord.get('id'));
            store.getProxy().setExtraParam('level',3);
        });
    },
    /**
     * 添加菜单组
     */
    menuGropAddAction:function () {
        this.showMenuForm('新增菜单组',1);
    },
    /**
     * 添加菜单
     */
    menuAddAction:function () {
        this.showMenuForm('新增菜单',2);
    },
    /**
     * 添加功能菜单
     */
    menuFuncAddAction:function () {
        this.showMenuForm('新增功能菜单',3);
    },
    /**
     * 显示菜单form
     * @param title
     * @param level
     * @param id
     */
    showMenuForm:function (title,level,id) {
        var form = Ext.widget('MenuForm');
        form.down('hiddenfield[name=level]').setValue(level);
        if(level==1){
            form.down('textfield[name=path]').setVisible(false);
            form.down('textfield[name=path]').setDisabled(true);
        }
        if(level == 2){
           var row =  ExtGetSelectRow(this.getMenuGroupGridView());
            if(!row){
                ExtAlert('','请选择菜单组');
                return;
            }
            console.log(row.get('id'));
            form.down('hiddenfield[name=parentId]').setValue(row.get('id'));
        }
        if (level == 3){
            var row =  ExtGetSelectRow(this.getMenuGridView());
            if(!row){
                ExtAlert('','请选择菜单');
                return;
            }
            form.down('hiddenfield[name=parentId]').setValue(row.get('id'));
        }
        form.showFormWin(title,id);
    },
    /**
     * 提交表单
     * @param btn
     * @param eOpts
     */
    addMenuOkBtnClick:function (btn,eOpts) {
        form = btn.up('form');
        var id = form.down('hiddenfield[name=id]').getValue();
        var level = form.down('hiddenfield[name=level]').getValue();
        var grid;
        if (level==1){
            grid = this.getMenuGroupGridView();
        }else if(level==2){
            grid = this.getMenuGridView();
        }else{
            grid = this.getMenuFuncGridView();
        }
        var url = getUrl('/menu/add');
        if(id){
            url = getUrl('/menu/edit');
        }
        ExtSubmitForm(form,url,function () {
            form.closeFormWin();
            grid.getStore().reload();
        });
    },
    menuGroupGridViewSelect:function (grid, record, index, eOpts) {
        //{params:{parentId:record.get('id')}}
        this.getMenuGridView().getStore().load();
    },
    menuGridViewSelect:function (grid, record, index, eOpts) {
        this.getMenuFuncGridView().getStore().load();
    },
    menuGroupGridViewItemcontextmenu:function (grid, record, item, index, e, eOpts) {
        e.preventDefault();
        var menu = this.getMenuRightMenu();
        if(!menu){
            menu = Ext.widget('MenuRightMenu');
        }
        menu.level = 1;
        menu.showAt(e.getXY());
    },
    menuGridViewItemcontextmenu:function (grid, record, item, index, e, eOpts) {
        e.preventDefault();
        var menu = this.getMenuRightMenu();
        if(!menu){
            menu = Ext.widget('MenuRightMenu');
        }
        menu.level = 2;
        menu.showAt(e.getXY());
    },
    menuFuncGridViewItemcontextmenu:function (grid, record, item, index, e, eOpts) {
        e.preventDefault();
        var menu = this.getMenuRightMenu();
        if(!menu){
            menu = Ext.widget('MenuRightMenu');
        }
        menu.level = 3;
        menu.showAt(e.getXY());
    },
    /**
     * 编辑菜单
     * @param menu
     * @param item
     * @param e
     * @param eOpts
     */
    menuEditMenuItemClick:function (menu, item, e, eOpts) {
        var level = menu.parentMenu.level;
        var grid;
        var titleMsg = '';
        if(level == 1){
            grid = this.getMenuGroupGridView();
            titleMsg = '编辑菜单组';
        }else if(level == 2){
            grid = this.getMenuGridView();
            titleMsg = '编辑菜单';
        }else{
            grid = this.getMenuFuncGridView();
            titleMsg = '编辑功能菜单';
        }
        var row =  ExtGetSelectRow(grid);
        this.showMenuForm(titleMsg,level,row.get('id'));
    },
    /**
     * 删除菜单
     * @param menu
     * @param item
     * @param e
     * @param eOpts
     */
    menuDeleteMenuItemClick:function (menu, item, e, eOpts) {
        var level = menu.parentMenu.level;
        var grid;
        var tipMsg = '';
        if(level == 1){
            grid = this.getMenuGroupGridView();
            tipMsg = '是否确定删除该菜单组?';
        }else if(level == 2){
            grid = this.getMenuGridView();
            tipMsg = '是否确定删除该菜单';
        }else{
            grid = this.getMenuFuncGridView();
            tipMsg = '是否确定删除该功能菜单';
        }
        var row =  ExtGetSelectRow(grid);
        if(!row){
            ExtAlert('','请选择要删除的菜单');
            return;
        }
        Ext.Msg.confirm('',tipMsg,function (btn) {
            if (btn=='yes'){
                //确定删除
                ExtAjaxSubmit({
                    url:getUrl('/menu/delete'),
                    method:'POST',
                    params:{
                        id:row.get('id')
                    },
                    masktarget:grid
                },function (data) {
                    if(data.code==0){
                        grid.getStore().reload();
                    }
                });
            }
        });
    }
});